import django_filters

from merchants.models import MerchantBill, WaivePaymentMerchant, MerchantMonthlyBill, MerchantSettlementRecord
from orders.models import OrderInfo, AfterSaleApplicationForm, OrderGoods, Express
from goods.models import Goods, LaunchedGoods


class MerchantBillFilter(django_filters.rest_framework.FilterSet):
    '''商户账单'''

    receipts_time_begin = django_filters.DateTimeFilter(field_name="receipts_time__date", lookup_expr='gte')
    receipts_time_end = django_filters.DateTimeFilter(field_name="receipts_time__date", lookup_expr='lte')
    receipts_time = django_filters.DateTimeFilter(field_name="receipts_time__date", lookup_expr='lte')
    bill_state = django_filters.BaseInFilter(field_name='bill_state', lookup_expr='in')
    payable_amount_min = django_filters.NumberFilter(field_name="payable_amount", lookup_expr='gte')
    payable_amount_max = django_filters.NumberFilter(field_name="payable_amount", lookup_expr='lte')
    channel_id = django_filters.BaseInFilter(field_name='channel_id', lookup_expr='in')
    bill_type = django_filters.BaseInFilter(field_name='bill_type', lookup_expr='in')

    class Meta:
        model = MerchantBill
        fields = ('receipts_time_begin', 'receipts_time_end', 'bill_state', 'payable_amount_min', 'payable_amount_max', 'receipts_time', 'channel_id',
                  'bill_type')


class WaivePaymentMerchantFilter(django_filters.rest_framework.FilterSet):
    '''账单过滤'''

    business_no = django_filters.BaseInFilter(field_name='business_no', lookup_expr='in')

    class Meta:
        model = WaivePaymentMerchant
        fields = ('business_no', )


class OrderInfoFilter(django_filters.rest_framework.FilterSet):
    '''订单过滤'''

    bill_id = django_filters.BaseInFilter(field_name='bill_id', lookup_expr='in')
    business_no = django_filters.BaseInFilter(field_name='merchant__wyfMerchantID', lookup_expr='in')
    state = django_filters.BaseInFilter(field_name='state', lookup_expr='in')
    order_species = django_filters.BaseInFilter(field_name='order_species', lookup_expr='in')
    create_time_begin = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='gte')
    create_time_end = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='lte')
    payment_time_begin = django_filters.DateTimeFilter(field_name="payment_time__date", lookup_expr='gte')
    payment_time_end = django_filters.DateTimeFilter(field_name="payment_time__date", lookup_expr='lte')

    class Meta:
        model = OrderInfo
        fields = ('bill_id', 'business_no', 'create_time_begin', 'create_time_end', 'state', 'order_species',
                  'payment_time_begin', 'payment_time_end')


class GoodsFilter(django_filters.rest_framework.FilterSet):

    is_use_point = django_filters.BaseInFilter(field_name='is_use_point', lookup_expr='in')
    price_min = django_filters.NumberFilter(field_name="price", lookup_expr='gte')
    price_max = django_filters.NumberFilter(field_name="price", lookup_expr='lte')
    business_no = django_filters.BaseInFilter(field_name='merchant__wyfMerchantID', lookup_expr='in')
    is_launched = django_filters.BaseInFilter(field_name='is_launched', lookup_expr='in')

    class Meta:
        model = Goods
        fields = ('is_use_point', 'is_launched', 'price_min', 'price_max', 'business_no')


class MerchantMonthlyBillFilter(django_filters.rest_framework.FilterSet):

    settlement_amount_min = django_filters.NumberFilter(field_name="settlement_amount", lookup_expr='gte')
    settlement_amount_max = django_filters.NumberFilter(field_name="settlement_amount", lookup_expr='lte')
    receipts_time_begin = django_filters.DateTimeFilter(field_name="receipts_time__date", lookup_expr='gte')
    receipts_time_end = django_filters.DateTimeFilter(field_name="receipts_time__date", lookup_expr='lte')

    class Meta:
        model = MerchantMonthlyBill
        fields = ('settlement_amount_min', 'settlement_amount_min', 'receipts_time_begin', 'receipts_time_end')


class MerchantSettlementRecordFilter(django_filters.rest_framework.FilterSet):

    settlement_time_begin = django_filters.DateTimeFilter(field_name="settlement_time__date", lookup_expr='gte')
    settlement_time_end = django_filters.DateTimeFilter(field_name="settlement_time__date", lookup_expr='lte')
    settlement_time = django_filters.DateTimeFilter(field_name="settlement_time__date", lookup_expr='gt')
    status = django_filters.BaseInFilter(field_name='status', lookup_expr='in')
    channel_id = django_filters.BaseInFilter(field_name='channel_id', lookup_expr='in')
    is_bind = django_filters.BaseInFilter(field_name='is_bind', lookup_expr='in')

    class Meta:
        model = MerchantSettlementRecord
        fields = ('settlement_time_begin', 'settlement_time_end', 'status', 'channel_id', 'is_bind', 'settlement_time')


class LaunchedGoodsFilter(django_filters.rest_framework.FilterSet):
    '''商品过滤'''

    create_time_begin = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='gte')
    create_time_end = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='lte')
    goods_id = django_filters.BaseInFilter(field_name='goods_id', lookup_expr='in')

    class Meta:
        model = LaunchedGoods
        fields = ('create_time_begin', 'create_time_end', 'goods_id')


class AfterSaleApplicationFormFilter(django_filters.rest_framework.FilterSet):
    '''申请单过滤'''

    order_id = django_filters.BaseInFilter(field_name='order_id', lookup_expr='in')

    class Meta:
        model = AfterSaleApplicationForm
        fields = ('order_id', )


class OrderGoodsFilter(django_filters.rest_framework.FilterSet):

    is_commented = django_filters.BaseInFilter(field_name='is_commented', lookup_expr='in')
    goods_id = django_filters.BaseInFilter(field_name='goods_id', lookup_expr='in')

    class Meta:
        model = OrderGoods
        fields = ('is_commented', 'goods_id')


class ExpressFilter(django_filters.rest_framework.FilterSet):

    deliverystatus = django_filters.BaseInFilter(field_name='deliverystatus', lookup_expr='in')
    create_time_begin = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='gte')
    create_time_end = django_filters.DateTimeFilter(field_name="create_time__date", lookup_expr='lte')
    sign_time_begin = django_filters.DateTimeFilter(field_name="sign_time__date", lookup_expr='gte')
    sign_time_end = django_filters.DateTimeFilter(field_name="sign_time__date", lookup_expr='lte')

    class Meta:
        model = Express
        fields = ('deliverystatus', 'create_time_begin', 'create_time_end', 'sign_time_begin', 'sign_time_end')